# -*- coding: utf-8 -*-
#
#   setup.py
#
#   Copyright (c) 2010, Anexandr Shmakov <uno.kms@gmail.com>
#   All rights reserved.
#
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions are met:
#
#   1. Redistributions of source code must retain the above copyright notice,
#      this list of conditions and the following disclaimer.
#   2. Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#   POSSIBILITY OF SUCH DAMAGE.

import os
import sys
import glob
import subprocess
from distutils.core import setup
from distutils.command.install_data import install_data
from distutils.command.build import build
from distutils.dep_util import newer
from distutils.log import info

app_name = "snapfly"
version = "0.6"

PO_DIR = "po"
MO_DIR = os.path.join("build", "mo")

class BuildLocales(build):
  def run(self):
    build.run(self)

    for po in glob.glob(os.path.join(PO_DIR, "*.po")):
      lang = os.path.basename(po[:-3])
      mo = os.path.join(MO_DIR, lang, app_name + ".mo")

      directory = os.path.dirname(mo)
      if not os.path.exists(directory):
        os.makedirs(directory)

      if newer(po, mo):
        info("compiling %s -> %s" % (po, mo))
        try:
          rc = subprocess.call(["msgfmt", po, "-o", mo])
          if rc != 0:
            raise Warning, "msgfmt returned %d" % rc
        except Exception, e:
          print "Building gettext files failed."
          print "%s: %s" % (type(e), str(e))
          sys.exit(1)

class Install(install_data):
  def run(self):
    self.data_files.extend(self._find_mo_files())
    install_data.run(self)

  def _find_mo_files(self):
    data_files = []
    for mo in glob.glob(os.path.join(MO_DIR, "*", app_name + ".mo")):
        lang = os.path.basename(os.path.dirname(mo))
        dest = os.path.join("share", "locale", lang, "LC_MESSAGES")
        data_files.append((dest, [mo]))
    return data_files

setup(name=app_name,
      version=version,
      description="Simple PyGTK menu app",
      author="Anexandr Shmakov",
      author_email="uno.kms@gmail.com",
      url="http://code.google.com/p/snapfly/",
      license="GNU General Public License (GPL)",
      classifiers=["Environment :: X11 Applications :: GTK",
                   "Intended Audience :: End Users/Desktop",
                   "License :: OSI Approved :: GNU General Public License (GPL)",
                   "Operating System :: POSIX :: Linux",
                   "Programming Language :: Python :: 2.6",
                   "Topic :: Desktop Environment",
                   "Topic :: Utilities"],
      py_modules=["snapfly_core","ui"],
      scripts=["snapfly"],
      cmdclass = {'build': BuildLocales, 'install_data': Install},
      data_files=[("bin", ["snapfly-show"]),
		  ("share/icons/hicolor/16x16/apps", ["icons/hicolor/16x16/apps/snapfly.png"]),
		  ("share/icons/hicolor/22x22/apps", ["icons/hicolor/22x22/apps/snapfly.png"]),
		  ("share/icons/hicolor/24x24/apps", ["icons/hicolor/24x24/apps/snapfly.png"]),
		  ("share/icons/hicolor/32x32/apps", ["icons/hicolor/32x32/apps/snapfly.png"]),
		  ("share/icons/hicolor/36x36/apps", ["icons/hicolor/36x36/apps/snapfly.png"]),
		  ("share/icons/hicolor/48x48/apps", ["icons/hicolor/48x48/apps/snapfly.png"]),
		  ("share/icons/hicolor/64x64/apps", ["icons/hicolor/64x64/apps/snapfly.png"]),
		  ("share/icons/hicolor/72x72/apps", ["icons/hicolor/72x72/apps/snapfly.png"]),
		  ("share/icons/hicolor/96x96/apps", ["icons/hicolor/96x96/apps/snapfly.png"]),
		  ("share/icons/hicolor/128x128/apps", ["icons/hicolor/128x128/apps/snapfly.png"]),
		  ("share/icons/hicolor/192x192/apps", ["icons/hicolor/192x192/apps/snapfly.png"]),
		  ("share/icons/hicolor/256x256/apps", ["icons/hicolor/256x256/apps/snapfly.png"]),
		  ("share/icons/hicolor/scalable/apps", ["icons/hicolor/scalable/apps/snapfly.svg"])]
      )
